//
//  SecureFileRW.m
//  Beat Bin Laden
//
//  Created by How Charles on 11-11-5.
//  Copyright 2011 hust. All rights reserved.
//

#import "SecureFileRW.h"
#import "storage.h"

@implementation SecureFileRW

+ (NSString *)dataFilePath
{
	return [storage dataFilePathCommon:SECURE_FILE_NAME];
}




+ (NSString *)UserDataFilePath
{
	return [storage dataFilePathCommon:SECURE_USER_INFO_FILE_NAME];
}


/*
 文件名作为参数，产生全路径
 */
+ (NSString *)customDataFilePath:(NSString *)fileName
{
	//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	//NSString *documentsDirectory = [paths objectAtIndex:0];
	return [storage dataFilePathCommon:fileName];
}


/*
 从固定的文件中读取解密后的数据
 */
+(NSData *)readDecryptedDataFromSecureFile:(NSString*)filePath
{
	
	///////1.从加密文件获取加密数据NSData
	NSData *encryptedData=[NSData dataWithContentsOfFile:filePath];
	
	/////2.将NSData解密
	NSData *decryptedData=[self decryptData:encryptedData];
	

	return decryptedData;
}


+ (void)readFile:(int *)praArray Size:(int)size path:(NSString*)fileFullPath
{
	
	
	NSString *filePath = fileFullPath;
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
	{
		NSData *decryptedData=[self readDecryptedDataFromSecureFile:fileFullPath];
		if (decryptedData == nil) {
			/////文件被篡改或者用户企图修改数据
			[self punishCracker];
			return ;
		}
		
		/////1.将decryptedData转换为array
		NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:decryptedData];
		

		
		
		////NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];  ///原始的没有加密的做法
		
		
		NSString * str;
		
		for (int i=0; i<size; i++) {
			str = [array objectAtIndex:i];
			//myNSLog(@"---------viewDidLoad---------5,str=%@\n\r",str);

			praArray[i]=[str intValue];

			
		}
		
		//[array release];
		
	}
	else {
		[self fileNotExist];
		
		/////数据清零
		for (int i=0; i<size; i++) {
			praArray[i]=0;
		}
	}
	
	
}


+(void)fileNotExist
{
	////文件 不存在
	///[self WriteDemoFile:3];   //test code
}

+(void)punishCracker
{
	//////惩罚破解的用户
}

+(void)showAlert
{
	//////显示警告信息，或者结束应用程序
	
}

+ (void)WriteFile:(int *)praArray Size:(int)size path:(NSString *)filePath
{
	/////////////////首先需要将int数组转化为字符串数组
	NSMutableArray *array = [[NSMutableArray alloc] init];
	
	
	NSString * str;
	//NSNumber * nsNum;
	for (int i=0; i<size; i++) {
		str = [NSString stringWithFormat:@"%d",praArray[i]];
		
		[array addObject:str];
	}
	
	
	[self writeArrayToSecureFile:array path:filePath];
	
	[array release];
	
}


+(void)writeArrayToSecureFile:(NSMutableArray *)array  path:(NSString *)filePath
{
	/////1.将array转换为NSData
	NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
	
	/////2.将NSData加密
	NSData *encryptedData=[self encryptData:data];
	
	//////3.讲加密后的数据写入到文件
	[encryptedData writeToFile:filePath atomically:YES];
}




/*
 返回加密后的数据
 */
+(NSData *)encryptData:(NSData *)data
{
	NSData* key = [SECURE_PRIVATE_KEY dataUsingEncoding:NSUTF8StringEncoding];
    NSData* iv = [SECURE_PRIVATE_IV dataUsingEncoding:NSUTF8StringEncoding];
	
	return [FBEncryptorAES encryptData:data key:key iv:iv];
    
}

/*
 返回解密后的数据
 */
+(NSData *)decryptData:(NSData *)encryptedData
{
	NSData* key = [SECURE_PRIVATE_KEY dataUsingEncoding:NSUTF8StringEncoding];
    NSData* iv = [SECURE_PRIVATE_IV dataUsingEncoding:NSUTF8StringEncoding];
	
	return [FBEncryptorAES decryptData:encryptedData key:key iv:iv];
}


+ (void)WriteDemoFile:(int)ItemCounts path:(NSString *)filePath
{
	
	NSMutableArray *array = [[NSMutableArray alloc] init];
	
	
	NSString * str;
	//NSNumber * nsNum;
	for (int i=0; i<ItemCounts; i++) {
		str = [NSString stringWithFormat:@"%d",10*i];
		[array addObject:str];
	}
	
	/*
	 [array addObject:field2.text];
	 [array addObject:field3.text];
	 [array addObject:field4.text];
	 */
	
	/*  
	 //////////////========示例代码====//////////////////////////
	 //////////////========示例代码====//////////////////////////
	 //////////////========示例代码====//////////////////////////
	 NSData NSArray 转换   
	 
	 NSArray *arr1 = [[NSArray alloc]initWithObjects:@"0",@"5",nil];
	 
	 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr1];
	 
	 NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
	 
	 
	 myNSLog(@"->%@",[arr1 objectAtIndex:1]);
	 myNSLog(@"->%@",[arr2 objectAtIndex:1]);
	 */
	
	
	[self writeArrayToSecureFile:array path:filePath];
	
	
	////[array writeToFile:[self dataFilePath] atomically:YES];  //原来的写法，直接将array写入到plist文件，这样的文件是没有加密的，容易被用户修改或破解
	
	[array release];
	
}



@end
